<template>
  <div class="login-page">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>登入</span>
        </div>
      </template>
      <el-form :model="form" label-width="100px" @submit.prevent="handleLogin">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">登入</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {ref} from 'vue'
import {ElNotification} from "element-plus";
import {useRouter} from "vue-router";
export default {
  setup() {
    const form = ref({username: '', password: ''})
    const router = useRouter()
    const handleLogin = () => {
      if (form.value.username === 'admin' || form.value.password === 'admin') {
        localStorage.setItem('isAuthenticated', 'true')
        ElNotification({
          title: '成功',
          message: '登入成功',
          type: 'success',
        });
        router.push('/page2')
      } else {
        ElNotification({
          title: '错误',
          message: '用户名或密码错误，请重试',
          type: 'error',
        });
      }
    }
    return {form, handleLogin}
  }
}
</script>
<style scoped>
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 32vh;
  background: #f0f2f5;
}
.box-card {
  width: 400px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.card-header {
  text-align: center;
}
</style>